﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using CardLogistics.Model.PutOutCard;

namespace CardLogistics.DAL.PutOutCard
{
    /// <summary>
    /// 半成品/成品卡申领_数据访问类
    /// </summary>
    public class PutOutApplyData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_PutOutApply", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据需求单ID查询基本信息
        /// </summary>
        /// <param name="demandId">需求单Id</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoByDemandId(string demandId)
        {
            return GetList(" DemandId = '" + demandId + "' ", "*");
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }

        /// <summary>
        /// 初始化时调用卡类型
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetCardTypeList()
        {
            Dictionary<string, string> cardTypeList = new Dictionary<string, string>();
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult("select id,cardtypename from basic_cardtype where  STATUS=0 and ISDELETE=0");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i]["id"].ToString();
                string cardtypename = dt.Rows[i]["cardtypename"].ToString();
                cardTypeList.Add(id, cardtypename);
            }
            return cardTypeList;
        }

        /// <summary>
        /// 编辑时调用卡类型
        /// </summary>
        /// <param name="demandId">需求Id</param>
        /// <returns></returns>
        public Dictionary<string, string> GetTypeList(string demandId)
        {
            Dictionary<string, string> cardTypeList = new Dictionary<string, string>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select distinct B.id,B.cardtypename from basic_cardtype B");
            strSql.Append(" inner join v_putoutapply P on P.Cardtype= B.ID");
            strSql.Append(" where  B.STATUS=0 and B.ISDELETE=0 and P.DEMANDID ='" + demandId + "'");
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult(strSql.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i]["id"].ToString();
                string cardtypename = dt.Rows[i]["cardtypename"].ToString();
                cardTypeList.Add(id, cardtypename);
            }
            return cardTypeList;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PutOutApply GetModel(string rId)
        {
            PutOutApply model = new PutOutApply();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["APPLYNO"] != null)
                {
                    model.APPLYNO = row["APPLYNO"].ToString();
                }
                if (row["APPLYDATE"] != null && row["APPLYDATE"].ToString() != "")
                {
                    model.APPLYDATE = DateTime.Parse(row["APPLYDATE"].ToString());
                }
                if (row["DEMANDID"] != null)
                {
                    model.DEMANDID = row["DEMANDID"].ToString();
                }
                if (row["REMARKS"] != null)
                {
                    model.REMARKS = row["REMARKS"].ToString();
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["ISDELETE"] != null && row["ISDELETE"].ToString() != "")
                {
                    model.ISDELETE = decimal.Parse(row["ISDELETE"].ToString());
                }
                if (row["ADDTIME"] != null && row["ADDTIME"].ToString() != "")
                {
                    model.ADDTIME = DateTime.Parse(row["ADDTIME"].ToString());
                }
                if (row["ISSUBMIT"] != null && row["ISSUBMIT"].ToString() != "")
                {
                    model.ISSUBMIT = decimal.Parse(row["ISSUBMIT"].ToString());
                }

                if (row["CARDTYPE"] != null)
                {
                    model.CARDTYPE = row["CARDTYPE"].ToString();
                }
            
                if (row["CARDNUMBER"] != null && row["CARDNUMBER"].ToString() != "")
                {
                    model.CARDNUMBER = decimal.Parse(row["CARDNUMBER"].ToString());
                }

                if (row["DemandNo"] != null && row["DemandNo"].ToString() != "")
                {
                    model.DemandNo =row["DemandNo"].ToString();
                }
                if (row["CardTypeName"] != null)
                {
                    model.CardTypeName = row["CardTypeName"].ToString();
                }
                if (row["DemandTotal"] != null)
                {
                    model.DemandTotal = decimal.Parse(row["DemandTotal"].ToString());
                }
                if (row["NotApplyTotal"] != null)
                {
                    model.NotApplyTotal = decimal.Parse(row["NotApplyTotal"].ToString());
                }
                if (row["CardTypeName"] != null)
                {
                    model.CardTypeName = row["CardTypeName"].ToString();
                }
                if (row["DemandEndDate"] != null)
                {
                    model.DemandEndDate = DateTime.Parse(row["DemandEndDate"].ToString());
                }
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(PutOutApply model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
                    new OracleParameter("vAPPLYNO", OracleType.VarChar,50),
					new OracleParameter("vAPPLYDATE", OracleType.DateTime),
					new OracleParameter("vDEMANDID", OracleType.VarChar,50),
					new OracleParameter("vREMARKS", OracleType.VarChar),
					new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEDATE", OracleType.DateTime),
					new OracleParameter("vISDELETE", OracleType.Number,4),
					new OracleParameter("vADDTIME", OracleType.DateTime),
                    new OracleParameter("vISSUBMIT", OracleType.Number,4),
                    new OracleParameter("vCARDTYPE", OracleType.VarChar,50),
					new OracleParameter("vCARDNUMBER", OracleType.Number,4)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.APPLYNO;
                parameters[2].Value = model.APPLYDATE;
                parameters[3].Value = model.DEMANDID;
                parameters[4].Value = model.REMARKS;
                parameters[5].Value = model.LASTUPDATEUSER;
                parameters[6].Value = model.LASTUPDATEDATE;
                parameters[7].Value = model.ISDELETE;
                parameters[8].Value = model.ADDTIME;
                parameters[9].Value = model.ISSUBMIT;
                parameters[10].Value = model.CARDTYPE;
                parameters[11].Value = model.CARDNUMBER;
                string StrOutPar = DbHelperSQL.RunProcedure("P_PutOutApply_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(PutOutApply model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("vID", OracleType.VarChar,50),
                    new OracleParameter("vAPPLYNO", OracleType.VarChar,50),
					new OracleParameter("vAPPLYDATE", OracleType.DateTime),
					new OracleParameter("vDEMANDID", OracleType.VarChar,50),
					new OracleParameter("vREMARKS", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar,50),
					new OracleParameter("vLASTUPDATEDATE", OracleType.DateTime),
					new OracleParameter("vISDELETE", OracleType.Number,4),
					new OracleParameter("vADDTIME", OracleType.DateTime),
                    new OracleParameter("vISSUBMIT", OracleType.Number,4),
                   	new OracleParameter("vCARDTYPE", OracleType.VarChar,50),
					new OracleParameter("vCARDNUMBER", OracleType.Number,4),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.APPLYNO;
                parameters[2].Value = model.APPLYDATE;
                parameters[3].Value = model.DEMANDID;
                parameters[4].Value = model.REMARKS;
                parameters[5].Value = model.LASTUPDATEUSER;
                parameters[6].Value = model.LASTUPDATEDATE;
                parameters[7].Value = model.ISDELETE;
                parameters[8].Value = model.ADDTIME;
                parameters[9].Value = model.ISSUBMIT;
                parameters[10].Value = model.CARDTYPE;
                parameters[11].Value = model.CARDNUMBER;
                parameters[12].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_PutOutApply_Update", parameters, parameters[12]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_PutOutApply_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
